Partial Public Class pages_Datas_default : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property RequestSeriesId() As Integer
        Get
            Return CWeb.RequestInt("seriesId")
        End Get
    End Property
    Public ReadOnly Property RequestColumns() As List(Of Integer)
        Get
            Return CWeb.RequestInts("columns")
        End Get
    End Property
    Public ReadOnly Property RequestDecisions() As List(Of Integer)
        Get
            Return CWeb.RequestInts("decisions")
        End Get
    End Property
#End Region

#Region "Form"
    Public Property CurrentSeriesId() As Integer
        Get
            Return CDropdown.GetInt(ddSeries)
        End Get
        Set(ByVal value As Integer)
            CDropdown.SetValue(ddSeries, value)
        End Set
    End Property
    Public Property CurrentColumns() As List(Of Integer)
        Get
            Dim list As New List(Of Integer)()
            list.AddRange(CDropdown.SelectedInts(cblRaw))
            list.AddRange(CDropdown.SelectedInts(cblDM))
            list.AddRange(CDropdown.SelectedInts(cblDI))
            list.AddRange(CDropdown.SelectedInts(cblTrading))
            'list.Add(EData.Notes)
            Return list
        End Get
        Set(ByVal value As List(Of Integer))
            CDropdown.SetValues(cblDI, value)
            CDropdown.SetValues(cblDM, value)
            CDropdown.SetValues(cblRaw, value)
            CDropdown.SetValues(cblTrading, value)
        End Set
    End Property
    Public Property CurrentDecisions() As List(Of Integer)
        Get
            Return CDropdown.SelectedInts(cblDecision)
        End Get
        Set(ByVal value As List(Of Integer))
            CDropdown.SetValues(cblDecision, value)
        End Set
    End Property
    Public ReadOnly Property CurrentColumnsAsEData() As List(Of EData)
        Get
            Dim ints As List(Of Integer) = CurrentColumns
            Dim cols As New List(Of EData)(ints.Count)
            For Each i As Integer In ints
                cols.Add(CType(i, EData))
            Next
            Return cols
        End Get
    End Property
    Public ReadOnly Property CurrentDecisionsAsEDecision() As List(Of EDecision)
        Get
            Dim ints As List(Of Integer) = CurrentDecisions
            Dim cols As New List(Of EDecision)(ints.Count)
            For Each i As Integer In ints
                cols.Add(CType(i, EDecision))
            Next
            Return cols
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property Series() As CSeries
        Get
            Return CSeries.Cache.GetById(CurrentSeriesId)
        End Get
    End Property
    Public ReadOnly Property [Datas]() As CDataList
        Get
            If IsNothing(Series) Then Return New CDataList
            If Request.Url.Host.ToLower.Contains("localhost") Then Series.Datas.DoCalcs()
            Return Series.Datas
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
        'Dropdowns
        CDropdown.AddEnums(cblDecision, GetType(EDecision))

        Add(cblRaw, EData.Open)
        Add(cblRaw, EData.High)
        Add(cblRaw, EData.Low)
        Add(cblRaw, EData.Close)

        Add(cblDI, EData.PlusDI)
        Add(cblDI, EData.MinusDI)
        Add(cblDI, EData.ADX_Item)
        Add(cblDI, EData.ADX)
        Add(cblDI, EData.StopLong)
        Add(cblDI, EData.StopShort)
        'Add(cblDI, EData.StopLoss)

        Add(cblDM, EData.PlusDM)
        Add(cblDM, EData.MinusDM)
        Add(cblDM, EData.AvgPlusDM)
        Add(cblDM, EData.AvgMinusDM)
        Add(cblDM, EData.TrueRange)
        Add(cblDM, EData.ATR)

        Add(cblTrading, EData.Outlook)
        Add(cblTrading, EData.Held)
        Add(cblTrading, EData.Total)
        Add(cblTrading, EData.Exposure)
        Add(cblTrading, EData.Long)
        Add(cblTrading, EData.Short)
        Add(cblTrading, EData.LongQty)
        Add(cblTrading, EData.ShortQty)
        Add(cblTrading, EData.PointValue)
        Add(cblTrading, EData.Returns)
        Add(cblTrading, EData.Notes)
        'Add(cblTrading, EData.ExpAdjRet)

        ddSeries.DataTextField = "SeriesName"
        ddSeries.DataValueField = "SeriesId"
        ddSeries.DataSource = CSeries.Cache
        ddSeries.DataBind()

        'Restore state
        CurrentSeriesId = RequestSeriesId
        CurrentColumns = RequestColumns
        CurrentDecisions = RequestDecisions
        chkDecisionLevels.Checked = (CDropdown.SelectedInts(cblDecision).Count = cblDecision.Items.Count)
        chkDirectionalIndex.Checked = (CDropdown.SelectedInts(cblDI).Count = cblDI.Items.Count)
        chkDirectionalMvmt.Checked = (CDropdown.SelectedInts(cblDM).Count = cblDM.Items.Count)
        chkRawData.Checked = (CDropdown.SelectedInts(cblRaw).Count = cblRaw.Items.Count)
        chkSimTrading.Checked = (CDropdown.SelectedInts(cblTrading).Count = cblTrading.Items.Count)

        'Display 
        ctrlDatas.Display(Me.Datas, CurrentColumnsAsEData, CurrentDecisionsAsEDecision)

        lnkBack.NavigateUrl = CSitemap.Seriess
        lnkEdit.NavigateUrl = CSitemap.SeriesEdit(Series.SeriesId)
        lnkNewSeries.NavigateUrl = CSitemap.SeriesAdd()
        lnkNewData.NavigateUrl = CSitemap.DataAdd(CurrentSeriesId, CurrentColumns, CurrentDecisions)
    End Sub
    Private Shared Sub Add(ByVal dd As ListControl, ByVal data As EData)
        CDropdown.Add(dd, data.ToString(), CInt(data))
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub dd_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddSeries.SelectedIndexChanged, cblDecision.SelectedIndexChanged, cblDI.SelectedIndexChanged, cblDM.SelectedIndexChanged, cblRaw.SelectedIndexChanged, cblTrading.SelectedIndexChanged
        ctrlDatas.Info.PageIndex = 0
        Response.Redirect(CSitemap.Datas(CurrentSeriesId, CurrentColumns, CurrentDecisions, ctrlDatas.Info))
    End Sub
    Protected Sub chkDecisionLevels_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDecisionLevels.CheckedChanged
        CDropdown.SelectAll(cblDecision, chkDecisionLevels.Checked)
        dd_SelectedIndexChanged(sender, e)
    End Sub
    Protected Sub chkDirectionalIndex_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDirectionalIndex.CheckedChanged
        CDropdown.SelectAll(cblDI, chkDirectionalIndex.Checked)
        dd_SelectedIndexChanged(sender, e)
    End Sub
    Protected Sub chkDirectionalMvmt_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDirectionalMvmt.CheckedChanged
        CDropdown.SelectAll(cblDM, chkDirectionalMvmt.Checked)
        dd_SelectedIndexChanged(sender, e)
    End Sub
    Protected Sub chkRawData_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRawData.CheckedChanged
        CDropdown.SelectAll(cblRaw, chkRawData.Checked)
        dd_SelectedIndexChanged(sender, e)
    End Sub
    Protected Sub chkSimTrading_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSimTrading.CheckedChanged
        CDropdown.SelectAll(cblTrading, chkSimTrading.Checked)
        dd_SelectedIndexChanged(sender, e)
    End Sub
#End Region

#Region "Event Handlers - UserControl"
    Private Sub ctrl_ExportClick() Handles ctrlDatas.ExportClick
        Datas.ExportToCsv(Response, Series.SeriesName, CurrentColumnsAsEData, CurrentDecisionsAsEDecision)
    End Sub
    Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlDatas.ResortClick
        Response.Redirect(CSitemap.Datas(CurrentSeriesId, CurrentColumns, CurrentDecisions, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
    End Sub
#End Region

End Class
